package com.pig4cloud.pigx.common.customize.utils;

import cn.hutool.core.util.ArrayUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pig4cloud.pigx.common.customize.contants.SystemConfigConstants;
import com.pig4cloud.pigx.common.customize.i18n.constants.I18nMessages;
import com.pig4cloud.pigx.common.customize.i18n.util.MessageUtils;
import com.pig4cloud.pigx.common.customize.utils.id.IdWorker;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.Map;

/**
 * 初始化对象工具类
 *
 * @since 2022/8/14 15:58
 */
@Component
@AllArgsConstructor
public class CommonUtil {

    private final IdWorker idWorker;
    private final ObjectMapper objectMapper;

    private static String replaceMessage(String message, HashMap<String, String> map) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            message = message.replace(entry.getKey(), entry.getValue());
        }
        ;
        return message;
    }

    /**
     * 获得雪花id
     *
     * @return java.lang.Long
     * @author gong_da_kai
     * @since 2021/11/22
     * @since 1.0.0
     */
    public Long getId() {
        return idWorker.nextId();
    }

    /**
     * 抛出异常，"操作失败"
     *
     * @author gong_da_kai
     * @since 2021/11/20
     * @since 1.0.0
     */
    public static void throwError() {
        throw new RuntimeException(MessageUtils.get(I18nMessages.OPERATION_FAILURE));
    }

    /**
     * 获得分页开始下标
     *
     * @param pageNum  页码
     * @param pageSize 条数
     * @return {@link int}
     * @since 2022/8/26 19:25
     */
    public static int getStartIndex(int pageNum, int pageSize) {
        if (pageNum == 0) {
            pageNum = 1;
        }

        return (pageNum - 1) * pageSize;
    }

    /**
     * 获得分页结束下标
     *
     * @param pageNum  页码
     * @param pageSize 条数
     * @return {@link int}
     * @since 2022/10/10 17:03
     */
    public static int getEndIndex(int pageNum, int pageSize) {
        if (pageNum == 0) {
            pageNum = 1;
        }

        return pageNum * pageSize;
    }

    /**
     * 对象 -> json
     *
     * @param o 对象
     * @return {@link String}
     * @since 2022/8/26 20:54
     */
    public String toJson(Object o) {
        try {
            return objectMapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * json -> 对象
     *
     * @param json json
     * @param type class
     * @return {@link T}
     * @since 2022/8/26 20:57
     */
    public <T> T parseJson(String json, Class<T> type) {
        try {
            return objectMapper.readValue(json, type);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 设置USDT相关BigDecimal的精度
     *
     * @param decimal 金额
     * @return {@link BigDecimal}
     * @since 2022/10/15 17:18
     */
    public static BigDecimal setUSDTScale(BigDecimal decimal) {
        return decimal.setScale(SystemConfigConstants.amountScale, SystemConfigConstants.mainCurRoundingMode);
    }

    /**
     * 校验ip地址格式
     *
     * @param ip ip
     * @since 2022/11/3 14:00
     */
    public static void matchIp(String ip) {
        if (!RegexUtil.ip(ip)) {
            throw new RuntimeException(MessageUtils.get(I18nMessages.FORMAT_ERROR,
                    MessageUtils.get(I18nMessages.IP_ADDRESS),
                    " " + ip));
        }
    }

    /**
     * 获取参数
     *
     * @param paramMap 参数集合
     * @param name     参数名称
     * @param remove   是否移除参数
     * @return {@link String}
     * @since 2022/11/12 17:28
     */
    public static String getParamString(Map<String, String[]> paramMap, String name, boolean remove) {
        String[] values = paramMap.get(name);
        if (ArrayUtil.isEmpty(values)) {
            return null;
        }

        if (remove) {
            paramMap.remove(name);
        }

        return values[0];
    }

    /**
     * 毫秒转化为LocalDateTime
     *
     * @param millis 毫秒
     * @return {@link LocalDateTime}
     * @since 2023/2/9 16:35
     */
    public static LocalDateTime millis2LocalDateTime(long millis) {
        Instant instant = Instant.ofEpochMilli(millis);
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDateTime();
    }
}
